package com.patrol.logisticsmanage.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.MainActivity;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.R2;
import com.patrol.logisticsmanage.net.AppConfig;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.canteen.utils.ToastUtils;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HomeFragment extends Fragment {

    @BindView(R.id.card_list)
    CardView cardList;
    @BindView(R.id.tv_message)
    ImageView tvMessage;
    @BindView(R.id.card_notice)
    CardView cardNotice;
    @BindView(R.id.card_repairs)
    CardView cardRepairs;

    @BindView(R.id.card_union)
    CardView cardUnion;
    Unbinder unbinder;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        BaseRequest.getInstance().getApiServise().appUrl(AppConfig.getString("userName"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Map>(getContext()) {
                    @Override
                    public void onSuccess(BaseReponse<Map> listBaseReponse) {
                        cardUnion.setOnClickListener(v -> ARouter.getInstance().build("/app/WebViewActivity")
                                .withString("title","一卡通")
                                .withString("url", listBaseReponse.getMessage()).navigation());
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {

                    }

                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {

                    }
                });
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, root);
        cardNotice.setOnClickListener(v -> ARouter.getInstance().build("/app/WebViewActivity")
                .withString("title","公告")
                .withString("url", MainActivity.url3).navigation());

        tvMessage.setOnClickListener(v -> ARouter.getInstance().build("/app/WebViewActivity")
                .withString("title","通知")
                .withString("url", MainActivity.url5).navigation());
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.card_list)
    public void onViewClicked() {
        ARouter.getInstance().build("/app/equip").navigation();
    }

//    @OnClick(R.id.tv_message)
//    public void onMessageClicked() {
//        ARouter.getInstance().build("/app/MessageActivity").navigation();
//    }

    @OnClick(R.id.card_repairs)
    public void onRepairsClicked() {
        ARouter.getInstance().build("/app/AppearEnterActivity").navigation();
    }
}